﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="Problem40.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation.  All rights reserved. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// </copyright>
// <summary>
//   Defines the Problem40 type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace EulerProblemsCSharp.Problems
{
    using System.Linq;

    /// <summary>
    /// Problem 40.
    /// </summary>
    public class Problem40 : ProblemBase
    {
        /// <summary>
        /// Gets Number.
        /// </summary>
        /// <value>
        /// The number.
        /// </value>
        public override int Number
        {
            get
            {
                return 40;
            }
        }

        /// <summary>
        /// Gets Description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        public override string Description
        {
            get
            {
                return "If dn represents the nth digit of the fractional part find the value of the following expression: d1 x d10 x d100 x d1000 x d10000 x d100000 x d1000000";
            }
        }

        /// <summary>
        /// Solves problem 40.
        /// </summary>
        /// <returns>
        /// The solution.
        /// </returns>
        public override object Solve()
        {
            int i = 0;
            int[] positions = { 1, 10, 100, 1000, 10000, 100000, 1000000 };

            var query = from number in new SequenceGenerator().Decimal(1000000)
                        select new { number, position = ++i }
                        into numbers 
                                join p in positions on numbers.position equals p 
                                select numbers.number;

            var answer = query.Aggregate(1, (x, y) => x * y);

            return answer;
        }
    }
}